﻿<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<link rel="stylesheet" type="text/css" media="screen" href="css/smoothness/jquery-ui-1.10.3.custom.min.css" />	<!-- jqueryUI CSS -->
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />							<!-- jqGrid CSS -->
<link rel="stylesheet" type="text/css" media="screen" href="css/selfTemplate.css" />						<!-- 本專案自行定義 CSS -->

<script src="js/jquery-1.9.1.min.js"></script>		<!-- jQuery 核心 -->
<script src="js/grid.locale-tw.js" type="text/javascript"></script>											<!-- jqGrid 語系檔 -->
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>										<!-- jqGrid 核心 -->
<script src="js/jquery-ui-1.10.3.custom.min.js" type="text/javascript"></script>							<!-- jqueryUI 核心 -->
<script src="js/jquery.cookie.js"></script>																	<!-- 操作cookie用 -->
<script src="js/widgets.js" type="text/javascript"></script>							<!-- jqueryUI 核心 -->

<title>維護主機</title>

<script type="text/javascript">

$(document).ready(function() {
	if ($.cookie("userId") == null) {
		alert("請先登入系統");
		location.assign("index.html");
	} else {
		$("#a_form #loginUser").val( $.cookie("userId") );
		$("#a_form #isSU").val( $.cookie("isSU") );
		/*	先查詢有那些子系統供 Select	*/
		querySubSys("host");
		
		/*	綁定 下拉吧 選擇的事件 */ 
		selectSubSys("QueryHost");
		
		$("#gd").jqGrid({
		   	colNames:["主機IP", "主機名稱","PORT","帳號","密碼"],
		   	colModel:[
		   		{name:"hostIP",index:"hostIP", editable:true, edittype:"text",editrules:{custom:true,required:true,custom_func:function(){
		   			var htip = $("input#hostIP").val();
		   			return [checkIP(htip),"請輸入格式正確的IP值"];
		   		}},editoptions: {size:15, maxlength: 15}},
		   		{name:"hostName",index:"hostName", editable:true, edittype:"text",editrules:{custom:true,required:true,custom_func:function(){
		   			var selectedValue = $("option:selected");
		   			var isAllSeclected = true;
		   			var noSelectItem = "";
		   			$.each(selectedValue,function(i,res){
		   				var val = res.value;
		   				if (val == "noSelect") {
		   					isAllSeclected = false;
		   				}
		   			});
		   			return [isAllSeclected,"尚有未選擇的欄位"];
		   		}}},
		   		{name:"port",index:"port", editable:true, edittype:"text",editrules:{integer:true,required:true},editoptions: {size:5, maxlength: 5}},
		   		{
		   			name:"loginId", 
		   			editable:true, 
		   			edittype:"text"
		   		},
		   		{
		   			name:"loginPWD", 
		   			editable:true,
		   			hidden: true, hidedlg: true, editrules: { edithidden: true },
		   			edittype:"password"
		   		}
		   	],
		   	rowNum: 6,
		   	pager: "#pager",
		   	forceFit: true,
			rownumbers: true,
		   	viewrecords: true,	
			caption: "維護主機"
		}).jqGrid("navGrid","#pager",{add:true,edit:true,del:true,search:false,refresh:false},
		{	// Edit
			url: "ManipulateGridHosts",
			mtype: "POST",
			recreateForm: true,
			beforeShowForm: function () {
		        $('<a href="#">連線測試<span class="ui-icon ui-icon-disk"></span></a>').click( 
		        		function() {
		        			$("#p_ip").val( $("#hostIP").val() ); 
		        			$("#p_id").val( $("#loginId").val() );
		        			$("#p_pwd").val( $("#loginPWD").val() );
		        			$.post("TestConnection",$("#p_form").serializeArray(),function(res) {
		        				if (res == 1) {
		        					alert("連線成功!");
		        				} else {
		        					alert("連線失敗!");
		        				}
		        			},"Text");
		            }).addClass("fm-button ui-state-default ui-corner-all fm-button-icon-left")
		              .prependTo("#Act_Buttons>td.EditButton");
		    },
			beforeInitData : function(formid) {
				var bssId = $("select#bss option:selected").val();
				var subSysId = $("select#subsys option:selected").val();

				/**	變更前先將目前的主鍵的值寫入Form以傳入後台 **/
				$("#oriBssId").val(bssId);
				$("#oriSubSysId").val(subSysId);
				$("#oriHostIP").val( getColVal("gd","hostIP",false) );
				$("#oriPort").val( getColVal("gd","port",false) );
			},
			editData: { 
				bssId: function() { return $("select#bss option:selected").val(); },
				subSysId: function() { return $("select#subsys option:selected").val(); },
				oriBssId: function() { return $("select#bss option:selected").val(); },
				oriSubSysId: function() {	return $("select#subsys option:selected").val(); },
				oriHostIP: function() {	return $("#oriHostIP").val(); },
				oriPort: function() {	return $("#oriPort").val(); },
				loginId: function() {	return $("#loginId").val(); },
				loginPWD: function() {	return $("#loginPWD").val(); },
				currentUser: function() { return $.cookie("userId"); }
			}
		},
		{	// Add
			url: "ManipulateGridHosts",
			mtype: "POST",
			editData: { 
				bssId: function() { return $("select#bss option:selected").val(); },
				subSysId: function() { return $("select#subsys option:selected").val(); },
				currentUser: function() { return $.cookie("userId"); }
			},	
			recreateForm: true,
			beforeShowForm: function () {
		        $('<a href="#">連線測試<span class="ui-icon ui-icon-disk"></span></a>').click( 
		        		function() {
		        			$("#p_ip").val( $("#hostIP").val() ); 
		        			$("#p_id").val( $("#loginId").val() );
		        			$("#p_pwd").val( $("#loginPWD").val() );
		        			$.post("TestConnection",$("#p_form").serializeArray(),function(res) {
		        				if (res == 1) {
		        					alert("連線成功!");
		        				} else {
		        					alert("連線失敗!");
		        				}
		        			},"Text");
		            }).addClass("fm-button ui-state-default ui-corner-all fm-button-icon-left")
		              .prependTo("#Act_Buttons>td.EditButton");
		    }
		},
		{	// Delete
			url: "ManipulateGridHosts",
			mtype: "POST",
			delData: { 
				hostIP: function() { return getColVal("gd","hostIP",false); },
				bssId: function() {	return getColVal("gd","bssId",true); },
				subSysId: function() {	return getColVal("gd","subSysId",true); },
				port: function() {	return getColVal("gd","port",false); },
				currentUser: function() { return $.cookie("userId"); }
			}
		});
		initPage("維護主機");
	}
}).tooltip();
</script>

</head>
<body>
	<div id="header"></div>
	<p>
	
	<form id="q_form">
		<input type="hidden" id="fc" name="fc" />
		<input type="hidden" id="bssId" name="bssId" />
		<input type="hidden" id="subSysId" name="subSysId" />
		<input type="hidden" id="envId" name="envId" />
		
		<!-- 用來進行 Update時使用 -->
		<input type="hidden" id="oriHostIP" name="oriHostIP" />
		<input type="hidden" id="oriBssId" name="oriBssId" />
		<input type="hidden" id="oriSubSysId" name="oriSubSysId" />
		<input type="hidden" id="oriPort" name="oriPort" />
		<input type="hidden" id="hostName" name="hostName" />
		
	</form>
	
	<form id="a_form">
		<input type="hidden" id="fc" name="fc" />	<!-- 供Servlet識別從那個UI來呼叫 (fromCall 縮寫) -->
		<input type="hidden" id="subSysId" name="subSysId" />
		<input type="hidden" id="loginUser" name="loginUser" />
		<input type="hidden" id="isSU" name="isSU" />
	</form>
	
	<!-- 用來測試連線是否可用 -->
	<form id="p_form">
		<input type="hidden" id="p_ip" name="ip" />
		<input type="hidden" id="p_id" name="id" />
		<input type="hidden" id="p_pwd" name="pwd" />
	</form>
	
	<br/>


	<table>
		<tr>
			<th class="right"><label for="bss">系統：</label></th>
			<td><select id="bss">
					<option value="MBMS">MBMS</option>
			</select></td>
		</tr>
		<tr>
			<th class="right"><label for="subsys">子系統：</label></th>
			<td><select id="subsys"><option id='dOpt'>--請選擇--</option></select>
			</td>
		</tr>
	</table>
	
	<p>
	<hr />
	<p>
	<table id="gd"></table>
	<div id="pager"></div>


</body>
</html>